﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using Hammock;
using Hammock.Web;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using MyMeetup.Controls;
using MyMeetup.Entities;
using MyMeetup.ViewModels;
using Newtonsoft.Json;
using Telerik.Windows.Controls;

namespace MyMeetup
{
    public partial class AlbumPage : PhoneApplicationPage
    {
        private AlbumPageViewModel _viewModel;
        private Album _album;
        private List<Photo> _photos;
        private bool networkError;

        public AlbumPage()
        {
            InitializeComponent();
            _viewModel = DataContext as AlbumPageViewModel;
        }



        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            

            base.OnNavigatedTo(e);
            if (_album == null && State.ContainsKey("CurrentAlbum"))
                _album = State["CurrentAlbum"] as Album;

        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            State["CurrentAlbum"] = _album;
        }


        private void ImageClick(object sender, RoutedEventArgs e)
        {
            var selectedImage = ((Button)sender).DataContext as Photo;
            NavigationService.Navigate(new Uri("/PhotoPage.xaml?&url=" + selectedImage.photo_link, UriKind.Relative));
            //FrameworkElement root = Application.Current.RootVisual as FrameworkElement;
            //root.DataContext = _photos;
        }

        private void AddPhoto(object sender, EventArgs e)
        {
            if (LicenseHelper.IsTrial())
            {
                NavigationService.Navigate(new Uri("/BuyNow.xaml", UriKind.Relative));
            }
            else
            {
                if (networkError)
                {
                    MessageBox.Show("A network error has been detected. Please try again later.", "Network Error",
                                    MessageBoxButton.OK);
                    return;
                }
                var task = new PhotoChooserTask();
                task.Completed += ImageObtained;
                task.Show();
            }
        }

        void ImageObtained(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                if (_viewModel.PostPhotoCommand.CanExecute("")) _viewModel.PostPhotoCommand.Execute(e);
            }
        }

        private void RadDataBoundListBox_ItemTap(object sender, Telerik.Windows.Controls.ListBoxItemTapEventArgs e)
        {
            var selectedImage = ((RadDataBoundListBox)sender).SelectedItem as Photo;
            NavigationService.Navigate(new Uri(string.Format("/RadControlsItem1.xaml?&url={0}&albumId={1}", selectedImage.photo_link, _viewModel.AlbumDetails.photo_album_id), UriKind.Relative));
        }

    }
}